package com.ding.api.controller.v1;

import com.alibaba.fastjson2.JSONObject;
import com.ding.api.dto.InsertUserDTO;
import com.ding.domain.Repository.StudentRepository;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/v1/test")
@Slf4j
@Api("TestController")
public class TestController {

    @Autowired
    public StudentRepository service;


    @ApiOperation("测试数据库r")
    @GetMapping("/mysql/r")
    public ResponseEntity<JSONObject> mysqlRead() {
        log.warn("被调用了！");
        return ResponseEntity
                .status(HttpStatus.OK)
                .body(new JSONObject()
                        .fluentPut("data", service.getAllStudent()));
    }

    @ApiOperation("测试数据库c")
    @PostMapping("/mysql/c")
    public JSONObject mysqlCreate(@RequestBody InsertUserDTO student) {
        System.out.println(student);
        return new JSONObject()
                .fluentPut("success", service.insertStudent(student));
    }

    @ApiOperation("测试数据库u")
    @PutMapping("/mysql/u")
    public JSONObject mysqlUpdate(@RequestBody JSONObject data) {
        String id = data.getString("id");
        String name = data.getString("name");
        return new JSONObject()
                .fluentPut("success", service.updateStudent(id, name));
    }

    @ApiOperation("测试数据库d")
    @DeleteMapping("/mysql/d")
    public JSONObject mysqlDelete(@RequestParam String id) {
        return new JSONObject()
                .fluentPut("success", service.deleteStudentById(id));
    }

    @ApiOperation("一对多")
    @GetMapping("/mysql/findScoreByName")
    public ResponseEntity<JSONObject> getScoreByUserId(@RequestParam String name) {
        JSONObject data = new JSONObject().fluentPut("data", service.getScoreByUserName(name));
        return ResponseEntity.ok(data);
    }

    @ApiOperation("多对多")
    @GetMapping("/mysql/findStudentByTeacher")
    public ResponseEntity<JSONObject> getStudentByTeacher(@RequestParam String id) {
        JSONObject data = new JSONObject().fluentPut("data", service.getStudentByTeacherId(id));
        return ResponseEntity.ok(data);
    }


    @ApiOperation("forEach")
    @GetMapping("/mysql/forEach")
    public ResponseEntity<JSONObject> getUserByIds(@RequestParam ArrayList<String> s) {
        return ResponseEntity.ok(new JSONObject().fluentPut("date", service.listStudent(s)));
    }

    @ApiOperation("if")
    @GetMapping("/mysql/if")
    public ResponseEntity<JSONObject> testIf(@RequestParam String name) {
        return ResponseEntity.ok(new JSONObject().fluentPut("date", service.testIf(name)));
    }

    @ApiOperation("multiInsert")
    @PostMapping("/mysql/multiInsert")
    public ResponseEntity<JSONObject> testIf(@RequestBody List<InsertUserDTO> list) {
        return ResponseEntity.ok(new JSONObject().fluentPut("date", service.multiInsert(list)));
    }

}

